from collections import deque
class Solution:
    def floodFill(self, e: List[List[int]], y: int, x: int, newColor: int) -> List[List[int]]:
        if newColor == e[y][x]:
            return e
            
        b=e[y][x]
        e[y][x]=newColor
        lists=deque([(y,x)])
        while lists:
            y,x = lists.popleft()
            lst = [(y+1, x), (y-1, x), (y, x+1), (y, x-1)]
            for m, n in lst:
                if 0<=m<len(e) and 0<=n<len(e[0]):
                    if e[m][n]==b:
                        lists.append((m,n))
                        e[m][n]=newColor
        return e
          
